package com.example.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.domain.Password;
import com.example.domain.Result;
import com.example.dao.AccountDao;
import com.example.domain.Account;
import com.example.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.UUID;

@Service
public class UserServiceImpl {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    public AccountDao accountDao;
    public static final String address = "http://rd3ehgzse.hn-bkt.clouddn.com/";
    public Result updatePicture(MultipartFile multipartFile,String email) {
        Account account = accountDao.selectById(email);
        String originalFilename = multipartFile.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String substring = originalFilename.substring(i - 1);
        String fileName = UUID.randomUUID() + substring;
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String oldPicture = ops.get(email);
        if(oldPicture != null){
            QiniuUtils.deleteFileFromQiniu(oldPicture);
        }
        ops.set(email,fileName);
        try {
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(),fileName);
            if(account.getImage() != null){
                String str = account.getImage().replaceAll("^(?:[^/]*/){3}(.*)$","$1");
                QiniuUtils.deleteFileFromQiniu(str);
            }
        }catch (IOException ex){
            ex.printStackTrace();
            return new Result(false,"文件上传失败！");
        }
        return new Result(true,fileName,"文件上传成功！");
    }
    public Result savePicture(Account account){
        UpdateWrapper<Account> wrapper = new UpdateWrapper<>();
        wrapper.eq("email",account.getEmail()).set("image",address+account.getImage());
        int i = accountDao.update(null,wrapper);
        redisTemplate.delete(account.getEmail());
        return new Result(i>0);
    }
    public Result getUser(HttpServletRequest request){
        HttpSession session = request.getSession();
        String account = (String) session.getAttribute("account");
        if(account != null){
            session.setMaxInactiveInterval(30 * 60);
            Account account1 = accountDao.selectById(account);
            return new Result(true, account1);
        }else {
            return new Result(false);
        }
    }
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("account");
        return new Result(true);
    }
    public Result updatePassword(String email, Password password){
        Account account = accountDao.selectById(email);
        String nowPassword =  account.getPassword();
        if(nowPassword.equals(password.getOldPassword())){
            UpdateWrapper<Account> wrapper = new UpdateWrapper<>();
            wrapper.eq("email", email).set("password", password.getPassword());
            int i = accountDao.update(null,wrapper);
            return new Result(i>0);
        }else {
            return new Result(false);
        }
    }
    public Result cancelPicture(Account account){
        if(account.getImage() == null) {
            return new Result(false);
        }else {
            redisTemplate.delete(account.getEmail());
            QiniuUtils.deleteFileFromQiniu(account.getImage());
            return new Result(true);
        }
    }
}
